/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wf.ui.cmps;

import bc.swing.pfrm.BaseParamModel;
import bc.swing.pfrm.FieldParamModel.ChangeListener;
import bc.swing.pfrm.Model;
import bc.swing.pfrm.Page;
import bc.swing.pfrm.ano.PageDef;
import bc.swing.pfrm.ano.Param;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import wf.model.Schedule;
import wf.units.DatabaseUnit;
import static java.util.Calendar.*;

/**
 *
 * @author bennyl
 */
@PageDef(layout=CalendarView.class, name="Scheduler")
public class SchedulingModel extends Model{
    public static final String SCHEDULES_PARAM = "Schedules";
    public static final String SELECTED_DATE_PARAM = "Selected Date";
    Calendar selectedDate;
    List<Schedule> viewedSchedules;

    public SchedulingModel() {
        selectedDate = Calendar.getInstance();
        viewedSchedules = new LinkedList<Schedule>();
    }
    
    public void setSelectedDate(Calendar selectedDate) {
        this.selectedDate = selectedDate;
        updateViewedSchedules();
    }

    public void updateViewedSchedules() {
        viewedSchedules = DatabaseUnit.UNIT.selectSchedulesForDay(selectedDate.get(DAY_OF_MONTH), selectedDate.get(MONTH)+1 , selectedDate.get(YEAR));
        syncToView(SCHEDULES_PARAM);
    }

    public void setViewedSchedules(List<Schedule> viewedSchedules) {
        this.viewedSchedules = viewedSchedules;
    }

    @Param(name=SELECTED_DATE_PARAM)
    public Calendar getSelectedDate() {
        return selectedDate;
    }

    @Param(name=SCHEDULES_PARAM)
    public List<Schedule> getViewedSchedules() {
        return viewedSchedules;
    }

    @Override
    public void whenPageCreated(Page page) {
        page.getParam(SELECTED_DATE_PARAM).addChangeListener(new ChangeListener() {

            @Override
            public void onChange(BaseParamModel source, Object newValue, Object deltaHint) {
                System.out.println("Selected Date Changed To: " + newValue);
            }
        });
    }
    
}
